package com.jy.shenghuilogistics.service;

import org.springframework.stereotype.Service;
import com.jy.shenghuilogistics.bean.Dispatchinfo;
import com.jy.shenghuilogistics.bean.DouDispatch;
import com.jy.shenghuilogistics.bean.Employee;
import com.jy.shenghuilogistics.bean.Trucks;
import com.jy.shenghuilogistics.dao.EmployeeMapper;
import com.jy.shenghuilogistics.dao.TruckMapper;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TruckService {
    @Resource
    TruckMapper truckMapper;
    @Resource
    EmployeeMapper employeeMapper;
    /**
     * 查询一次调度所有携带的运单
     * @param id
     * @return
     */
    public List<DouDispatch> getDocByDisId(Integer id) {
        return truckMapper.selDocuByDispa(id);
    }
    /**
     * 查询调度信息
     * @param d
     * @return
     */
    public List<Dispatchinfo> selDispatch(Dispatchinfo d) {
        return truckMapper.selDispatch(d);
    }
    /**
     * 查询车辆最新一次调度所有携带的运单
     * @param id
     * @return
     */
    public List<DouDispatch> getDocByDispa(Integer id) {
        Integer disId =  truckMapper.selNewestDispaByTruck(id).getDispatchId();
        return truckMapper.selDocuByDispa(disId);
    }
    /**
     * 查询站点所有待接收货车
     * @param name
     * @return
     */
    public List<Trucks> getWaitTruckBySite(String name) {
        return truckMapper.selWaitTruckBySite(name);
    }
    /**
     * 添加调度车辆附带的运单
     * @param doc,dispatchId
     * @return
     */
    public Integer addDocDispa(List<Integer> doc,Integer dispatchId) {
        Integer result = 0;
        for(Integer docId : doc) {
            DouDispatch d = new DouDispatch();
            d.setDocId(docId);
            d.setDispatchId(dispatchId);
            result += truckMapper.insDouDispatch(d);

        }
        return result;
    }
    /**
     * 添加调度信息
     * @param d
     * @return
     */
    public Integer addDispa(Dispatchinfo d) {
        truckMapper.insDispatchinfo(d);
        return d.getDispatchId();
    }
    /**
     * 车辆数据修改
     * @param t
     * @return
     */
    public Integer updTruck(Trucks t) {
        if(t.getEmpId() != null && t.getEmpId() == 0) {
            t.setEmpId(null);
        }
        return truckMapper.updTrucks(t);
    }
    /**
     * 车辆模糊查询
     * @param t
     * @return
     */
    public List<Trucks> getTruck(Trucks t) {
        return truckMapper.selTruck(t);
    }

    /**
     * 添加车辆
     * @param t
     * @return
     */
    public String addTruck(Trucks t) {
        String result = "success";
        if(truckMapper.insTrucks(t)<1) {
            return  "error";
        }
        if(t.getEmpId() != null && t.getEmpId() != 0) {
            Employee e = new Employee();
            e.setEmpId(t.getEmpId());
            e.setTruckId(t.getTruckId());
            if (employeeMapper.updEmp(e)<1) {
                return  "error";
            }
        }
        return result;
    }
}
